package com.xique.park.controller.web;

import com.xique.common.core.enums.PayStatus;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.request.web.parkingOrder.ParkingOrderRequest;
import com.xique.park.bean.vo.web.parkingOrder.ParkingOrderVo;
import com.xique.park.service.service.ExportService;
import com.xique.park.service.service.IParkingOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

/**
 * 停车流水订单controller
 *
 * @author wld
 * @date 2022/3/11 15:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/web/parkingOrder")
public class WebParkingOrderController {

    private final IParkingOrderService parkingOrderService;

    @Autowired
    private ExportService exportService;

    @Log(title = "分页查询全部订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/list")
    public AjaxResult list(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询已支付订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/listByPaid")
    public AjaxResult listByPaid(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        parkingOrderRequest.setPayStatus(Arrays.asList(PayStatus.PAID.getStatus(), PayStatus.CASH_PAID.getStatus()));
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询免费放行订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/listByFree")
    public AjaxResult listByFree(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        parkingOrderRequest.setPayStatus(Collections.singletonList(PayStatus.FREE.getStatus()));
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询未确认放行订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/listByUnconfirmed")
    public AjaxResult listByUnconfirmed(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        parkingOrderRequest.setPayStatus(Collections.singletonList(PayStatus.WAIT_PAID.getStatus()));
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询取消放行订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/listByCancel")
    public AjaxResult listByCancel(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        parkingOrderRequest.setPayStatus(Collections.singletonList(PayStatus.CANCEL.getStatus()));
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "分页查询待支付订单记录")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @PostMapping(value = "/listByUnPaid")
    public AjaxResult listByUnPaid(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest) {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        parkingOrderRequest.setPayStatus(Collections.singletonList(PayStatus.WAIT_PAID.getStatus()));
        PageResult pageResult = parkingOrderService.selectPageList(parkingOrderRequest);
        return AjaxResult.success(pageResult);
    }

    @Log(title = "查询订单总数和订单总金额")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @GetMapping(value = "/statisticalInfo")
    public AjaxResult statisticalInfo() {
        ParkingOrderRequest parkingOrderRequest = new ParkingOrderRequest();
        parkingOrderRequest.setParkId(SecurityUtils.getParkId());
        ParkingOrderVo parkingOrderVo = parkingOrderService.selectStatisticalInfo(parkingOrderRequest);
        return AjaxResult.success(parkingOrderVo);
    }

    @Log(title = "查询已支付订单总数和订单总金额")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @GetMapping(value = "/statisticalInfoByPaid")
    public AjaxResult statisticalInfoByPaid() {
        ParkingOrderRequest parkingOrderRequest = new ParkingOrderRequest();
        parkingOrderRequest.setParkId(SecurityUtils.getParkId());
        parkingOrderRequest.setPayStatus(Arrays.asList(PayStatus.PAID.getStatus(), PayStatus.CASH_PAID.getStatus()));
        ParkingOrderVo parkingOrderVo = parkingOrderService.selectStatisticalInfo(parkingOrderRequest);
        return AjaxResult.success(parkingOrderVo);
    }

    @Log(title = "查询订单详情")
    @PreAuthorize(hasPermi = "parking:vehicle-order:list")
    @GetMapping(value = "/detailInfo/{parkingSerial}/{parkingOrderId}")
    public AjaxResult detailInfo(@PathVariable String parkingSerial,
                                 @PathVariable Long parkingOrderId) {
        if (StringUtils.isEmpty(parkingSerial) || null == parkingOrderId) {
            return AjaxResult.success();
        }
        ParkingOrderVo parkingOrderVo = parkingOrderService.selectDetailInfo(SecurityUtils.getParkId(), parkingSerial, parkingOrderId);
        return AjaxResult.success(parkingOrderVo);
    }

    @Log(title = "订单")
    @PreAuthorize(hasPermi = "parking:vehicle-order:export")
    @PostMapping(value = "/export")
    public void exportParkOrderList(@RequestBody(required = false) ParkingOrderRequest parkingOrderRequest, HttpServletResponse response) throws IOException {
        parkingOrderRequest = checkParam(parkingOrderRequest);
        exportService.exportParkOrderList(parkingOrderRequest, response);
    }

    @Log(title = "迁移数据")
    @PostMapping("/migrateAllDate")
    public AjaxResult migrateAllDate() {
        parkingOrderService.migrateDate(null);
        return AjaxResult.success();
    }

    @Log(title = "迁移指定项目的数据")
    @GetMapping("/migrateDateByPark/{parkId}")
    public AjaxResult migrateDateByPark(@PathVariable Long parkId) {
        parkingOrderService.migrateDate(parkId);
        return AjaxResult.success();
    }

    private ParkingOrderRequest checkParam(ParkingOrderRequest request) {
        if (request == null) {
            request = new ParkingOrderRequest();
        }
        int pageNum = request.getPageNum() == null ? 1 : request.getPageNum();
        int pageSize = request.getPageSize() == null ? 10 : request.getPageSize();
        if (request.getParkId() == null) {
            Long parkId = SecurityUtils.getParkId();
            if (parkId != null) {
                request.setParkId(parkId);
            }
        }
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        return request;
    }
}
